package com.distributed.tcp.enums;

import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

@AllArgsConstructor
@NoArgsConstructor
@Getter
public enum TcpPortEnum {
    DEVICE_A(28888, "Device-A"),
    DEVICE_B(28889, "Device-B");

    private int port;
    private String name;

    /**
     * 根据端口号获取对应的枚举实例
     */
    public static TcpPortEnum getByPort(int port) {
        for (TcpPortEnum portEnum : values()) {
            if (portEnum.port == port) {
                return portEnum;
            }
        }
        return null;
    }

    /**
     * 检查端口是否在监控列表中
     */
    public static boolean isWatchPort(int port) {
        return getByPort(port) != null;
    }

    /**
     * 获取所有监控端口号列表
     */
    public static List<Integer> getAllPorts() {
        return Arrays.stream(values())
                .map(TcpPortEnum::getPort)
                .collect(Collectors.toList());
    }

    /**
     * 根据端口号获取对应的名称
     */
    public static String getNameByPort(int port) {
        TcpPortEnum portEnum = getByPort(port);
        return portEnum != null ? portEnum.getName() : "未知端口";
    }

}
